<?php
class PeopleController extends BaseController {
	private $_peopleId;
	function init() {
		parent::init();
		
		if ($_SERVER['REQUEST_URI'] == '/mykitchen') {
			$this->checkLogin();
			$this->_peopleId = $this->_actorId;
		} else {
			$this->_peopleId = (int)$this->_getParam("peopleId");
		}
		$isMyself = $this->_peopleId == $this->_actorId;
		if ($this->_peopleId > 0) {
			$actor = new ActorEntity();
			if (!$actor->get($this->_peopleId)) notFound();
			$this->smarty->assign_by_ref("people", $actor->getDataArray());
		}
		
		// 厨房的名称
		$kitchenName = $isMyself ? "我的厨房": $actor->nickname."的厨房";
		$this->smarty->assign(array(
			"isMyself"=>$isMyself,
			"pageTitle"=> $kitchenName,
			"kitchenName" => $kitchenName,
		));
		
	}

	/**
	 * 做过的菜
	 *
	 */
	function doneAction() {
		// 做过的菜
		$pageSize = 9;
		require 'Xiachufang/logic/Food.php';
		$pageResult = FoodLogic::pagedGetActorFoodIds($this->_peopleId, $this->_pi, $pageSize);
		$foodList = array();
		if ($pageResult->total > 0) {
			foreach($pageResult->data as $id) {
				$food = new FoodEntity();
				if (!$food->get($id)) continue;
				$foodList[] = $food->getDataArray();
			}
		}
		$pageNavStr = $pageResult->render("/people/".$this->_peopleId."/done?page={0}");
		$this->smarty->assign_by_ref("foodList", $foodList);
		
		$this->smarty->assign('pageNavStr', $pageNavStr);
		$this->_tableShow($pageResult);
		
		$this->display('people/done.tpl');
	}
	/**
	 * 收藏的菜谱
	 *
	 */
	function favAction() {
		require_once 'Xiachufang/logic/Event.php';
		require_once 'Xiachufang/entity/Recipe.php';
		$pageSize = 9;
		$pageResult = EventLogic::pagedGetActorFavs($this->_peopleId, $this->_pi, $pageSize);
		$recipeList = array();
		if ($pageResult->total > 0) {
			foreach($pageResult->data as $id) {
				$recipe = new RecipeEntity();
				if (!$recipe->get($id)) continue;
				$recipeList[] = $recipe->getDataArray();
			}
		}
		$pageNavStr = $pageResult->render("/people/".$this->_peopleId."/fav?page={0}");
		$this->smarty->assign_by_ref("recipeList", $recipeList);
		$this->smarty->assign('pageNavStr', $pageNavStr);
		$this->_tableShow($pageResult);
		
		$this->display();
	}
	
	/**
	 * 编辑过的菜谱
	 *
	 */
	function wikiAction() {
		require_once 'Xiachufang/logic/Wiki.php';
		require_once 'Xiachufang/entity/Recipe.php';
		$pageSize = 9;
		$pageResult = WikiLogic::pagedGetActorWikiIds($this->_peopleId, 0, $this->_pi, $pageSize);
		$wikiList = array();
		foreach($pageResult->data as $row) {
			$wikiId = $row["recipe_wiki_id"];
			$wiki = new RecipeWikiEntity();
			if (!$wiki->get($wikiId)) continue;
			$recipeId = $row["recipe_id"];
			$recipe = new RecipeEntity();
			if (!$recipe->get($recipeId)) continue;
			$data = $recipe->getDataArray(array("recipeId","name"));
			$data["wikiId"] = $wikiId;
			$data["description"] = $wiki->description;
			$wikiList[] = $data;
		}
		$pageNavStr = $pageResult->render("/people/".$this->_peopleId."/wiki?page={0}");
		$this->smarty->assign_by_ref("wikiList", $wikiList);
		
		$this->_tableShow($pageResult);
		
		$this->display();
	}
	
	/**
	 * 打过的标签
	 *
	 */
	function tagsAction() {
		$tagNum = 0;
		//case 4: // 用户的标签。所有
		require_once 'Xiachufang/logic/Tag.php';
		//if ($typeId == 4) $tagNum = 0;
		$tagList = TagLogic::getActorTagClouds($tagNum, $this->_peopleId);
		foreach($tagList as &$tag) {
			$tag["size"] = TagLogic::getTagFontSize($tag["num"]);
		}
		$this->smarty->assign_by_ref("tagList", $tagList);
		$this->display();
	}
	
	/**
	 * 用来格式化数据适应表格的显示
	 *
	 * @param unknown_type $pageResult
	 */
	private function _tableShow($pageResult) {
		$num = count($pageResult->data);
		$leftTdList = array();
		if ($num > 0 && $num%3 != 0) {
			for($i = $num%3; $i < 3; $i++) {
				$leftTdList[] = $i;	
			}
		}
		
		$this->smarty->assign(array(
			"leftTdList"=>$leftTdList
		));
	}
	
	/**
	 * 用户资料
	 *
	 */
	function profileAction() {
		$this->smarty->assign(array(
			"curUrl"=>"/",
			"pageTitle"=>"个人资料",
			"pageDescription"=>$actor->nickname."的个人资料",
		));
		
		$this->display();
	}
	
	/**
	 * 修改密码
	 *
	 */
	function passwordAction() {
		if ($this->_peopleId != $this->_actorId) notFound();
		$this->smarty->assign(array(
			"curUrl"=>"/",
			"pageTitle"=>"修改密码",
		));
		$this->display();
	}
	
	/**
	 * 修改头像
	 *
	 */
	function faceAction() {
		if ($this->_peopleId != $this->_actorId) notFound();
		$this->smarty->assign(array(
			"curUrl"=>"/",
			"pageTitle"=>"修改头像",
		));
		$this->display();
	}

	/**
	 * 个人主页
	 *
	 */
	function indexAction() {
		$this->doneAction();
	}
}
?>